package com.example.presentshopping.ui.fragment.earnings

import BaseRecyclerAdapter
import BaseRecyclerHolder
import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.text.Html
import android.view.View
import android.widget.TextView
import com.example.presentshopping.R
import com.example.presentshopping.config.base.BaseMvpFragment
import com.example.presentshopping.config.bean.RefundetailBean
import com.example.presentshopping.mvp.presenter.PublicPresenter
import com.example.presentshopping.net.http.HttpRequest
import com.example.presentshopping.net.http.HttpURL
import com.example.presentshopping.ui.custom.RoundImageView
import com.example.presentshopping.utils.glide.GlideUtil
import com.example.presentshopping.utils.tool.GsonUtils
import com.yanzhenjie.recyclerview.SwipeRecyclerView
import kotlinx.android.synthetic.main.activity_refundetail.*

class RefundetailFragment(override var context: Context, var str: String) :BaseMvpFragment() {


    var refundetailBean : RefundetailBean?=null
    var baseRecyclerAdapter: BaseRecyclerAdapter<RefundetailBean.DataBean.ListBean>? = null
    var  recycler_view : SwipeRecyclerView?=null
    var  count_total: TextView ?=null
    private var sendMesPresenter: PublicPresenter? = null
    var page =1
    override fun initView(view: View?, savedInstanceState: Bundle?) {
        sendMesPresenter = PublicPresenter(context,false,"")
        sendMesPresenter?.attachView(this)
        sendMesPresenter?.setHaveCode(false)
        recycler_view =view?.findViewById(R.id.recycler_view)
        count_total =view?.findViewById(R.id.count_total)
        setAdap()
        OnsInit()
        recycler_view?.useDefaultLoadMore()
        recycler_view?.loadMoreFinish(false, true)
        recycler_view?.setLoadMoreListener {
            page++
            OnsInit()
        }
    }
    fun setAdap(){
        baseRecyclerAdapter = object :BaseRecyclerAdapter<RefundetailBean.DataBean.ListBean>(context,null,R.layout.item_refundetail){
            @SuppressLint("SetTextI18n")
            override fun convert(
                holder: BaseRecyclerHolder?,
                data: RefundetailBean.DataBean.ListBean,
                position: Int,
                isScrolling: Boolean,
                selectedPosition: Int
            ) {
                val from_type = holder?.getView<TextView>(R.id.from_type)
                val type = holder?.getView<TextView>(R.id.type)
                val total = holder?.getView<TextView>(R.id.total)
                val order_id = holder?.getView<TextView>(R.id.order_id)
                val  iv_thumb =holder?.getView<RoundImageView>(R.id.iv_thumb)
                GlideUtil.load(context,data.thumb.toString(),iv_thumb)
                //status 1收入 2消费
                var status = when(data.status){
                    1->{"+ "}
                    2->{"+ "}
                    else->""
                }
                total?.text = status + data.total.toString()
                from_type?.text = when(data.from_type){
                    1->{ "红包" }
                    2->{"分润"}
                    3->{"团队收益"}
                    4->{"退款"}
                    else -> ""
                }
                type?.text =data.create_time
                order_id?.text = "订单编号: "+data.order_id

            }

        }
        recycler_view?.adapter  = baseRecyclerAdapter
    }
    fun OnsInit(){
        sendMesPresenter?.netWorkRequestPost(
            HttpURL.Req_getBalanceChanger,
            HttpRequest.getTodatearnin(str,page,10))
    }

    override fun onDestroyView() {
        sendMesPresenter?.detachView()
        super.onDestroyView()
    }

    override fun onSuccess(data: String?) {
        refundetailBean = GsonUtils.getGson().fromJson(data,RefundetailBean().javaClass)
        recycler_view?.loadMoreFinish(false, true)

        baseRecyclerAdapter?.setData(refundetailBean?.data?.list)

        when(str){
            "1"->{
                count_total?.text =    Html.fromHtml("昨日退款: "+"<font color=\"#ff2555\">"+refundetailBean?.data?.today_total+"</font>"+"元 \n \n 总退款: "+"<font color=\"#ff2555\">"+refundetailBean?.data?.count_total+"</font>"+"元")

            }
            "2"->{
                count_total?.text =    Html.fromHtml("今日退款: "+"<font color=\"#ff2555\">"+refundetailBean?.data?.today_total+"</font>"+"元  \n \n 总退款: "+"<font color=\"#ff2555\">"+refundetailBean?.data?.count_total+"</font>"+"元")

            }
        }


    }

    override fun onFailure(e: Throwable?, code: Int, msg: String?, isNetWorkError: Boolean) {

    }

    override val layoutId: Int
        get() = R.layout.refundetail_fragment
}